package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 查询当前用户登录的所有地址信息
     * @return java.util.List<com.sky.entity.AddressBook>
     * @author zhupp
     * @create 2025/2/25
     **/

    @Override
    public List<AddressBook> getAddressBook() {
        // 获取到当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(userId);
        // 查询该该用户的地址簿
        List<AddressBook> addressBooks =  addressBookMapper.selectAddressByUserId(addressBook);
        return addressBooks;
    }

    /**
     * 查询默认地址
     * @return com.sky.entity.AddressBook
     * @author zhupp
     * @create 2025/2/25
     **/
    @Override
    public AddressBook getDefaultAddressBook() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(1);
        return addressBookMapper.selectDefaultAddress(addressBook);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return void
     * @author zhupp
     * @create 2025/2/25
     **/
    @Override
    public void addAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        // 判断该地址是否为第一个地址
        Long count = addressBookMapper.count();
        if (count == 0){
            addressBook.setIsDefault(1);
        }else {
            addressBook.setIsDefault(0);
        }
        addressBookMapper.insertAddressBook(addressBook);
    }

    /**
     * 根据地址id查询回显
     * @param id
     * @return com.sky.entity.AddressBook
     * @author zhupp
     * @create 2025/2/25
     **/
    @Override
    public AddressBook getAddressBookById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        return addressBookMapper.selectAddressByUserId(addressBook).get(0);
    }

    /**
     * 根据地址id修改地址
     * @param addressBook
     * @return void
     * @author zhupp
     * @create 2025/2/25
     **/
    @Override
    public void updateAddressBookById(AddressBook addressBook) {
        addressBookMapper.updateAddressBookById(addressBook);
    }
    /**
     * 根据地址id删除地址
     * @param id
     * @return void
     * @author zhupp
     * @create 2025/2/25
     **/
    @Override
    public void deleteAddressBookById(Long id) {
        addressBookMapper.deleteAddressBookById(id);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return void
     * @author zhupp
     * @create 2025/2/25
     **/
    @Override
    public void updateDefaultAddressBookById(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());

        // 将之前的默认地址取消
        addressBook.setIsDefault(0);
        addressBookMapper.updateCancleDefault(addressBook);

        // 设置默认地址
        addressBook.setIsDefault(1);
        addressBookMapper.updateAddressBookById(addressBook);
    }
}
